package com.ujs.edu.demo.controller;

import com.ujs.edu.demo.config.NeedLogin;
import com.ujs.edu.demo.dao.BcBooksMapper;
import com.ujs.edu.demo.dataobject.BcBooks;
import com.ujs.edu.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class PutController {

    @Autowired
    private BcBooksMapper bcBooksMapper;


    @RequestMapping(path = "put")
    public ModelAndView put() {
        ModelAndView view = new ModelAndView();

        List<BcBooks> bcBooks = bcBooksMapper.selectByMaktable(1);
        view.addObject("books", bcBooks);
        view.setViewName("put");
        return view;
    }

    //搜索
    @RequestMapping(path = "searchbook")
    @ResponseBody
    public ModelAndView searchBook(@RequestParam("input") String input) {
        Result result = new Result();

        ModelAndView view = new ModelAndView();

        List<BcBooks> bcBooks = bcBooksMapper.selectLike(input);
        view.addObject("books", bcBooks);
        for (int i = 0; i < bcBooks.size(); i++) {
            String bookName = bcBooks.get(i).getBookName();
            System.out.println(bookName);
        }
        view.setViewName("put");
        result.setStatus(1);
        result.setMsg("成功");
        return view;
    }

    //下架
    @RequestMapping(path = "/unmaktable")
    @ResponseBody
    public Result unmaktable(@RequestParam("bookId") Integer bookId) {
        Result result = new Result();
        int bc = bcBooksMapper.updateunmaktable(bookId);
        if (bc == 1) {
            result.setStatus(1);
            result.setMsg("下架成功");
        } else {
            result.setStatus(0);
            result.setMsg("下架失败");
        }
        return result;
    }

    //数据回填
    @RequestMapping(path = "/putbooksInfrom")
    public ModelAndView editbooks(@RequestParam("bookId") Integer bookId) {
        ModelAndView view = new ModelAndView();
        BcBooks bcBooks = bcBooksMapper.selectByPrimaryKey(bookId);
        view.addObject("books", bcBooks);
        view.setViewName("modbooks");

        return view;
    }

    //编辑上架书籍
    @RequestMapping(path = "/editbooks")
    @ResponseBody
    public Result editbooks(BcBooks bcBooks) {
        Result result = new Result();

        int bc = bcBooksMapper.updateInform(bcBooks);

        if (bc == 1) {
            result.setStatus(1);
            result.setMsg("更新成功");
        } else {
            result.setStatus(0);
            result.setMsg("更行失败");
        }
        return result;
    }


    //删除上架书籍
    @RequestMapping(path = "/delputbooks")
    @ResponseBody
    public Result delputbooks(@RequestParam("bookId") Integer bookId){
        Result result=new Result();

        int del=bcBooksMapper.deleteByPrimaryKey(bookId);
        if (del==1){
            result.setStatus(1);
            result.setMsg("删除成功");
        }else {
            result.setStatus(0);
            result.setMsg("删除失败");
        }
        return result;
    }
}